package managers;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import conn.Conn;

public class Manager {
	protected Statement stmt;
	protected CallableStatement cstm;
	protected ResultSet rst;
	
	public Manager() {
		
		try {
			stmt = Conn.getConn().createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	protected boolean execUpdate(String sql) {
		boolean res = false;
		
		try {
			if (stmt.executeUpdate(sql) > 0) res = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return res;
	}
	
	protected boolean execUpdate(String[] sqls) {
		boolean res = true;
		int s;
		int n = sqls.length;
		
		try {
			for (s = 0; s < n; s ++) {
				if (!(stmt.executeUpdate(sqls[s]) >= 0)) {
					res = false;
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			res = false;
		}
		
		return res;
	}
	
	protected boolean execUpdate(String sql0, String sql1) {
		String[] sqls = {sql0, sql1};
		return execUpdate(sqls);
	}
	
	protected boolean execUpdate(String sql0, String sql1, String sql2) {
		String[] sqls = {sql0, sql1, sql2};
		return execUpdate(sqls);
	}
	
	protected long getGenerateKey() {
		long key = 0;
		
		try {
			rst = stmt.getGeneratedKeys();
			if (rst.next()) key = rst.getLong(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return key;
	}
}